package com.wargame.engine;

import java.util.HashMap;
import java.util.List;


/**
 * 算子射击方法
 *
 * @param thisOpr 算子对象
 * @param action 算子动作
 * @return Object 交互信息，符合交互模型定义的HashMap<String, Object>类型
 * */

public Object CalloutShootAction(Operator thisOpr, Action action) throws ClassNotFoundException {

    int objID = (int) thisOpr.getAttri("ObjID");
    HashMap<String, Integer> remainBulletNums = (HashMap<String, Integer>) thisOpr.getAttri("RemainBulletNums");
    int weaponCoolTime = (int) thisOpr.getAttri("WeaponCoolTime");
    List<String> states = (List<String>) thisOpr.getAttri("States");
    int weaponUnfoldTime = (int) thisOpr.getAttri("WeaponUnfoldTime");
    String weaponUnfoldState = (String) thisOpr.getAttri("WeaponUnfoldState");
    String moveState = (String) thisOpr.getAttri("MoveState");
    String targetState = (String) thisOpr.getAttri("TargetState");
    boolean moveShoot = (boolean) thisOpr.getAttri("MoveShoot");
    int changeStateRemainTime = (int) thisOpr.getAttri("ChangeStateRemainTime");
    double speed = (double) thisOpr.getAttri("Speed");
    boolean stop = (boolean) thisOpr.getAttri("Stop");
    int[] curHex = (int[]) thisOpr.getAttri("CurHex");
    String color = (String) thisOpr.getAttri("Color");
    String subType = (String) thisOpr.getAttri("SubType");
    boolean keep = (boolean) thisOpr.getAttri("Keep");
    int blood = (int) thisOpr.getAttri("Blood");
    HashMap<String, Integer> observeDistance = (HashMap<String, Integer>) thisOpr.getAttri("ObserveDistance");
    List<String> validActions = (List<String>) thisOpr.getAttri("ValidActions");
    boolean flagForceStop = (boolean)  thisOpr.getAttri("FlagForceStop");
    double moveToStopRemainTime = (double) thisOpr.getAttri("MoveToStopRemainTime");

    int actionObjID = (int) action.getAttri("ObjId");
    String actionName = (String) action.getAttri("Name");
    String actionWeapon = (String) action.getAttri("Weapon");
    int actionTargetObjID = (int) action.getAttri("TargetObjID");

    Equipment equipment = thisOpr.getEquipment(actionWeapon);

    boolean isValid = true;
//        String bulletType = "Conventional";  //  只有常规弹药
    String bulletType = (String) equipment.getAttri("Bullet");
    int numOfBulletType = remainBulletNums.get(actionWeapon);

    if (objID != actionObjID) {
        System.out.println("无效动作：算子不为执行动作的算子");
        isValid = false;
    } else if (!actionName.equals("Shoot")) {
        System.out.println("无效动作：算子行动不是射击");
        isValid = false;
    }
    // 根据武器获取弹药名称
    // 需要更新Equipment类的用法
    else if (equipment == null || equipment.getAttri(actionWeapon) == null) {
        System.out.println("无效动作：算子没有装配射击动作使用的武器");
        isValid = false;
    } else if (!remainBulletNums.containsKey(actionWeapon) || numOfBulletType <= 0) {
        System.out.println("无效动作：没有可用弹药");
        isValid = false;
    } else if (weaponCoolTime > 0) {
        System.out.println("无效动作：武器处于冷却中");
        isValid = false;
    } else if (weaponUnfoldTime > 0) { // 武器正在展开或折叠
        System.out.println("无效动作：武器正在展开或折叠");
        isValid = false;
    } else if (weaponUnfoldState.equals("Fold")) { // 武器处于锁定状态
        System.out.println("无效动作：武器处于锁定状态");
        isValid = false;
    } else if (moveState.equals("March")) {  // 行军状态不可射击
        System.out.println("无效动作：射击算子处于行军状态");
        isValid = false;
    } else if (moveState.equals("March") && changeStateRemainTime > 0) {  // 行军状态转换过程中 TODO moveState换成targetState?
        System.out.println("无效动作：射击算子处于行军状态转换过程中");
        isValid = false;
    } else if(targetState .equals("March") && changeStateRemainTime>0) {
        System.out.println("无效动作：射击算子处于行军状态转换过程中");
        isValid = false;
    }else if (!moveShoot){//如果没有行进间射击能力
        if (targetState.equals("Shelt") && changeStateRemainTime >0){
            System.out.println("无效动作：无行进间射击能力的算子处于掩蔽状态转换过程中，不能射击");
            isValid = false;
        } else if (!stop) {
            System.out.println("无效动作：无行进间射击能力的算子不处于静止状态，不能射击");
        }

    }else if (moveShoot && !stop){//有行进间射击能力，且在机动中
        if (!subType.equals("CruissingMissile")){//巡飞弹对弹种无要求
                if (!actionWeapon.equals("MediumDirectGun")&& !actionWeapon.equals("LargeDirectGun")){//除了巡飞弹，如坦克，必须要使用大中号直瞄炮才能行进间射击
                    System.out.println("无效动作：使用的不是坦克主炮（大号、中号直瞄炮），不可行进间射击");
                    isValid = false;
                }
        }
    }else if (subType.equals("Infantry")&&keep){
        System.out.println("无效动作：算子是人员且被压制状态不可射击");
        isValid = false;
    }else if (flagForceStop||moveToStopRemainTime>0){
        System.out.println("无效动作：算子正在被强制停止,不能执行射击动作");
        isValid = false;
    }
    // 射击动作无效，从有效动作中移除并返回null
    if (!isValid) {
        // validActions.remove(actionName);  TODO validActions应该在SynchronizeToTime中更新
        return null;
    }
    // 可以射击
    
    remainBulletNums.put(bulletType, remainBulletNums.get(bulletType) - 1);
    thisOpr.setAttri("WeaponCoolTime", 76); //如何减少
    if (moveState.equals("Shelt"))// 如果算子正在掩蔽
    {
        thisOpr.setAttri("MoveState", "Normal"); // 取消掩蔽状态
    }
    if (moveShoot && (changeStateRemainTime > 0) && targetState.equals("Shelt"))// 算子有行进间射击能力，如果正在转掩蔽，中断转换
    {
        thisOpr.setAttri(changeStateRemainTime, 0);// 结束倒计时
        thisOpr.setAttri(targetState, thisOpr.getAttri("MoveState"));// 目标状态恢复为当前机动状态
    }

    // 生成 交互interactdata
    HashMap<String, Object> interactData = new HashMap<String, Object>();
    interactData.put("Name", "Shooted");
    interactData.put("ObjID", actionTargetObjID);
    interactData.put("AttObjID", objID);
    interactData.put("AttCurHex", curHex);
    interactData.put("AttColor", color);
    interactData.put("AttSubType", subType);
    interactData.put("Weapon", actionWeapon);
    interactData.put("AttBlood", blood);
    interactData.put("AttKeep", keep);
    interactData.put("AttSpeed", speed);
    interactData.put("AttObserveDistance", observeDistance);
    return interactData;
}

